﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SILAS.Entidades;

namespace SILAS.DAO
{
    public class SapDAO : MasterDAO, ISapDAO
    {

        public void registrarSap(SAP sap)
        {
            if (sap != null)
            {
                var sapi = new SAP();

                sapi.Nombre = sap.Nombre;
                sapi.FecConstruccion = sap.FecConstruccion;
                sapi.NroPilPublicas = sap.NroPilPublicas;
                sapi.NroFamBenef = sap.NroFamBenef;
                sapi.MontoInversion = sap.MontoInversion;
                sapi.SeClora = sap.SeClora;
                sapi.IdInstEjecutora = sap.IdInstEjecutora;
                sapi.IdInstSupervisora = sap.IdInstSupervisora;
                sapi.IdTipoSAP = sap.IdTipoSAP;

                foreach (var item in sap.FuenteAguas)
                {
                    var fuente = context.FuenteAguas.Find(item.Id);
                    sapi.FuenteAguas.Add(fuente);
                }

                foreach (var item in sap.Sectors)
                {
                    var sector = context.Sectors.Find(item.Id);
                    sapi.Sectors.Add(sector);
                }

                context.SAPs.Add(sapi);
                context.SaveChanges();
            }
           
        }
        public void registrarSapSector(int idSap, int idSector)
        {
            var sector = context.Sectors.Find(idSector);
            var Sap = context.SAPs.Find(idSap);

            sector.SAPs.Add(Sap);
            context.SaveChanges();
        }
       
        public List<SAP> ListaSAPByCriterio(string term, int idSector)
        {
            var sector = context.Sectors.Find(idSector);
            var list = sector.SAPs.ToList();
 
            var query = (from p in context.SAPs
                        select p).ToList();

            var sapsDisponibles = query.Except(list);          
     
            if (term != null)
            {
                sapsDisponibles = sapsDisponibles.Where(p => p.Nombre.ToUpper().StartsWith(term.ToUpper()) ||
                                    p.Nombre.ToUpper().Contains(term.ToUpper())
                                    );
            }

            return sapsDisponibles.OrderBy(p => p.Nombre).Skip(0).Take(15).ToList();

        }



        public List<SAP> ListarAllSaps(int id)
        {
            var query = (from p in context.Sectors
                         where p.Id.Equals(id)
                         select p).SingleOrDefault();

             var sapDsector = query.SAPs.ToList();

            var q = (from p in context.SAPs
                     select p).ToList();

            var list = q.Except(sapDsector);

            return list.ToList();
        }

        public void modificarSap(SAP sap)
        {
            /*var sapi = new SAP();
           
             if (sap != null)
             {
                 sapi.Nombre = sap.Nombre;
                 sapi.FecConstruccion = sap.FecConstruccion;
                 sapi.NroPilPublicas = sap.NroPilPublicas;
                 sapi.NroFamBenef = sap.NroFamBenef;
                 sapi.MontoInversion = sap.MontoInversion;
                 sapi.SeClora = sap.SeClora;
                 sapi.IdInstEjecutora = sap.IdInstEjecutora;
                 sapi.IdInstSupervisora = sap.IdInstSupervisora;
                 sapi.IdTipoSAP = sap.IdTipoSAP;


             }*/
        }
        //        void eliminarSap(SAP sap);

        public List<SAP> ListarSapByIdsector(int id)
        {
            var query = (from p in context.Sectors
                         where p.Id.Equals(id)
                         select p).SingleOrDefault();
            
            return query.SAPs.ToList();
        }
        
        public List<InstSupervisora> getentiSupervisora()
        {
            var query = (from p in context.InstSupervisoras
                         select p).ToList();
            return query;
        }

        public List<InstEjecutora> getinstEjecutora()
        {
            var query = (from p in context.InstEjecutoras
                         select p).ToList();
            return query;
        }

        public List<TipoSAP> getiposap()
        {
            var query = (from p in context.TipoSAPs
                         select p).ToList();
            return query;
        }

      

        public void eliminarSap(int id)
        {
            var existe = context.SAPs.Find(id);

            if (existe != null)
            {
                context.SAPs.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
